home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / fsutil / fsutilHandleScavenge.c < prev    next >
C/C++ Source or Header  |  1990-10-10  |  5KB  |  208 lines

  1. /* 
  2.  * fsutilHandleScavenge.c --
  3.  *
  4.  *    Routines controlling the scavenging of file system handles.
  5.  *
  6.  * Copyright 1989 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/kernel/fsutil/RCS/fsutilHandleScavenge.c,v 9.1 90/10/08 13:17:46 mendel Exp $ SPRITE (Berkeley)";
  18. #endif not lint
  19.  
  20.  
  21. #include <sprite.h>
  22.  
  23. #include <fs.h>
  24. #include <vm.h>
  25. #include <rpc.h>
  26. #include <fsutil.h>
  27. #include <fsprefix.h>
  28. #include <fsNameOps.h>
  29. #include <fsio.h>
  30. #include <fsutilTrace.h>
  31. #include <fsStat.h>
  32. #include <sync.h>
  33. #include <timer.h>
  34. #include <proc.h>
  35. #include <trace.h>
  36. #include <hash.h>
  37. #include <fsrmt.h>
  38.  
  39.  
  40. /*
  41.  * Monitor for OkToScavenge and DoneScavenge
  42.  */
  43. static Sync_Lock scavengeLock = Sync_LockInitStatic("Fs:scavengeLock");
  44. #define LOCKPTR (&scavengeLock)
  45.  
  46. static Boolean OkToScavenge _ARGS_((void));
  47. static void DoneScavenge _ARGS_((void));
  48.  
  49.  
  50. /*
  51.  *----------------------------------------------------------------------------
  52.  *
  53.  * Fsutil_HandleScavengeStub --
  54.  *
  55.  *    This is a thin layer on top of Fsutil_HandleScavenge.  It is called
  56.  *    when L1-x is pressed at the keyboard, and also from Fsutil_HandleInstall
  57.  *    when a threashold number of handles have been created.
  58.  *
  59.  * Results:
  60.  *    None.
  61.  *
  62.  * Side effects:
  63.  *    Invokes the handle scavenger.
  64.  *
  65.  *----------------------------------------------------------------------------
  66.  *
  67.  */
  68. /*ARGSUSED*/
  69. void 
  70. Fsutil_HandleScavengeStub(data)
  71.     ClientData    data;    /* IGNORED */
  72. {
  73.     /*
  74.      * This is called when the L1-x keys are held down at the console.
  75.      * We set up a call to Fsutil_HandleScavenge, unless there is already
  76.      * an extra scavenger scheduled.
  77.      */
  78.     if (OkToScavenge()) {
  79.     Proc_CallFunc(Fsutil_HandleScavenge, (ClientData)FALSE, 0);
  80.     }
  81. }
  82.  
  83. Boolean        scavengerScheduled = FALSE;
  84. int        fsScavengeInterval = 2;            /* 2 Minutes */
  85. int        fsLastScavengeTime = 0;
  86.  
  87.  
  88. /*
  89.  *----------------------------------------------------------------------------
  90.  *
  91.  * Fsutil_HandleScavenge --
  92.  *
  93.  *    Go through all of the handles looking for clients that have crashed
  94.  *    and for handles that are no longer needed.  This expects to be
  95.  *    called by a helper kernel processes at regular intervals defined
  96.  *    by fsScavengeInterval.
  97.  *
  98.  * Results:
  99.  *    None.
  100.  *
  101.  * Side effects:
  102.  *    The handle-specific routines may remove handles.
  103.  *
  104.  *----------------------------------------------------------------------------
  105.  *
  106.  */
  107. /*ARGSUSED*/
  108. void
  109. Fsutil_HandleScavenge(data, callInfoPtr)
  110.     ClientData        data;            /* Whether to reschedule again
  111.                          */
  112.     Proc_CallInfo    *callInfoPtr;        /* Specifies interval */
  113. {
  114.     Hash_Search                hashSearch;
  115.     register    Fs_HandleHeader        *hdrPtr;
  116.  
  117.     /*
  118.      * Note that this is unsynchronized access to a global variable, which
  119.      * works fine on a uniprocessor.  We don't want a monitor lock here
  120.      * because we don't want a locked handle to hang up all Proc_ServerProcs.
  121.      */
  122.     fsLastScavengeTime = Fsutil_TimeInSeconds();
  123.  
  124.     Hash_StartSearch(&hashSearch);
  125.     for (hdrPtr = Fsutil_GetNextHandle(&hashSearch);
  126.      hdrPtr != (Fs_HandleHeader *) NIL;
  127.          hdrPtr = Fsutil_GetNextHandle(&hashSearch)) {
  128.      if (fsio_StreamOpTable[hdrPtr->fileID.type].scavenge !=
  129.          (Boolean (*)())NIL) {
  130.          (*fsio_StreamOpTable[hdrPtr->fileID.type].scavenge)(hdrPtr);
  131.      } else {
  132.          Fsutil_HandleUnlock(hdrPtr);
  133.      }
  134.     }
  135.     /*
  136.      * We are called in two cases.  A regular call background call is indicated
  137.      * by a TRUE data value, while an extra scavenge that is done in an
  138.      * attempt to free space is the other case.
  139.      */
  140.     if ((Boolean)data) {
  141.     /*
  142.      * Set up next background call.
  143.      */
  144.     callInfoPtr->interval = fsScavengeInterval * timer_IntOneMinute;
  145.     } else {
  146.     /*
  147.      * Indicate that the extra scavenger has completed.
  148.      */
  149.     callInfoPtr->interval = 0;
  150.     DoneScavenge();
  151.     }
  152. }
  153.  
  154. /*
  155.  *----------------------------------------------------------------------------
  156.  *
  157.  * OkToScavenge --
  158.  *
  159.  *    Checks for already active scavengers.  Returns FALSE if there
  160.  *    is already a scavenger.
  161.  *
  162.  * Results:
  163.  *    TRUE if there is no scavenging in progress.
  164.  *
  165.  * Side effects:
  166.  *    Sets scavengerScheduled to TRUE if it had been FALSE.
  167.  *
  168.  *----------------------------------------------------------------------------
  169.  *
  170.  */
  171. static ENTRY Boolean
  172. OkToScavenge()
  173. {
  174.     register Boolean ok;
  175.     LOCK_MONITOR;
  176.     ok = !scavengerScheduled;
  177.     if (ok) {
  178.     scavengerScheduled = TRUE;
  179.     }
  180.     UNLOCK_MONITOR;
  181.     return(ok);
  182. }
  183.  
  184. /*
  185.  *----------------------------------------------------------------------------
  186.  *
  187.  * DoneScavenge --
  188.  *
  189.  *    Called when done scavenging.  This clears the flag that indicates
  190.  *    an extra scavenger is present.
  191.  *
  192.  * Results:
  193.  *    None.
  194.  *
  195.  * Side effects:
  196.  *    Clears scavengerScheduled.
  197.  *
  198.  *----------------------------------------------------------------------------
  199.  *
  200.  */
  201. static ENTRY void
  202. DoneScavenge()
  203. {
  204.     LOCK_MONITOR;
  205.     scavengerScheduled = FALSE;
  206.     UNLOCK_MONITOR;
  207. }
  208.